package com.qf.service.impl;

import com.qf.entity.AppInfo;
import com.qf.entity.AppVersion;
import com.qf.enums.AppEnum;
import com.qf.exception.AppException;
import com.qf.mapper.AppVersionMapper;
import com.qf.service.AppInfoService;
import com.qf.service.AppVersionService;
import com.qf.vo.AppVersionVO;
import com.qf.vo.TableListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AppVersionServiceImpl implements AppVersionService {

    @Autowired
    private AppVersionMapper appVersionMapper;

    @Autowired
    private AppInfoService appInfoService;

    @Override
    public List<AppVersionVO> findAppVersion(Integer appId) {

        List<AppVersionVO> list = appVersionMapper.findAppVersionByAppId(appId);


        return list;
    }

    @Override
    public void addVersion(AppVersion appVersion) {
        //封装参数   apk_file_name

        AppInfo appInfo = appInfoService.findAppInfoById(appVersion.getAppId());

        String apkName = appInfo.getApkName();
        appVersion.setApkFileName(apkName+"-"+appVersion.getVersionNo());

        //保存版本信息
        int count = appVersionMapper.insertSelective(appVersion);
        if (count!=1){
            log.error("【添加APP版本信息】添加APP版本信息失败！ appVersion={}",appVersion);
            throw new AppException(AppEnum.APPVERSION_ADD_ERROR);
        }

        //修改
        AppInfo newAppInfo = new AppInfo();
        newAppInfo.setVersionId(appVersion.getId());
        newAppInfo.setId(appInfo.getId());

        appInfoService.updateVersionById(newAppInfo);

    }
}
